﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Utilities.Logging
{
    public partial class CrashReportForm : Form
    {
        #region Singleton Instance
        private CrashReportForm()
        {
            InitializeComponent();
        }

        private static CrashReportForm _instance = null;
        public static CrashReportForm Instance
        {
            get
            {
                if (_instance == null || (_instance != null && _instance.IsDisposed))
                    _instance = new CrashReportForm();
                return _instance;
            }
        }
        #endregion

        private void buttonSendReport_Click(object sender, EventArgs e)
        {
            ALogger.Instance.SendTxtLogFileWithEmail();
        }

        private void buttonContinue_Click(object sender, EventArgs e)
        {
            Close();
        }

        internal static void ShowReport(Exception ex, ALogger.Severity severity)
        {
            ALogger.Instance.LogException(ex, severity);

            CrashReportForm.Instance.labelMessage.Text = ex.Message;
            Image image = null;

            switch (severity)
            {
                case ALogger.Severity.None:
                    image = System.Drawing.SystemIcons.WinLogo.ToBitmap();
                    CrashReportForm.Instance.labelTitle.Text = "Mesaj";
                    break;
                case ALogger.Severity.Information:
                    image = System.Drawing.SystemIcons.Information.ToBitmap();
                    CrashReportForm.Instance.labelTitle.Text = "Dikkat";
                    break;
                case ALogger.Severity.Warning:
                    image = System.Drawing.SystemIcons.Warning.ToBitmap();
                    CrashReportForm.Instance.labelTitle.Text = "Uyarı";
                    break;
                case ALogger.Severity.Error:
                    image = System.Drawing.SystemIcons.Exclamation.ToBitmap();
                    CrashReportForm.Instance.labelTitle.Text = "Hata Oluştu";
                    break;
                case ALogger.Severity.Critical:
                    image = System.Drawing.SystemIcons.Error.ToBitmap();
                    CrashReportForm.Instance.labelTitle.Text = "Kritik Hata";
                    break;
                default:
                    break;
            }

            CrashReportForm.Instance.pictureBoxSeverityIcon.Image = image;
            CrashReportForm.Instance.Show();
        }

        void hyperLinkEdit1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            ALogger.Instance.ShowTxtLogFile();
        }

        private void CrashReportForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                Close();
        }
    }
}
